/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjs.liyq.service.impl;

import edu.fjs.liyq.dao.UserDao;
import edu.fjs.liyq.dao.impl.UserDaoImpl;
import edu.fjs.liyq.entity.User;
import edu.fjs.liyq.exception.AccountNotFoundException;
import edu.fjs.liyq.exception.IncorrectPasswordException;
import edu.fjs.liyq.service.UserService;
import java.util.List;

/**
 *
 * @author danny
 */
public class UserServiceImpl implements UserService{

    @Override
    public List<User> getAll() {
        UserDao dao = new UserDaoImpl();
        return dao.getAll();
    }

    @Override
    public User authenticate(User user) {
        UserDao dao = new UserDaoImpl();
        User userFromDB = dao.selectByAccount(user.getAccount());
        if (userFromDB == null)
            throw new AccountNotFoundException();
        if (!user.getPassword().equals(userFromDB.getPassword()))
            throw new IncorrectPasswordException();
        
        return userFromDB;
    }
    
}
